<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="装修情况" prop="decoration">
        <el-input
          v-model="queryParams.decoration"
          placeholder="请输入装修情况"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="挂牌价格" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入挂牌价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="挂牌日期" prop="releaseDate">
        <el-date-picker clearable
          v-model="queryParams.releaseDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择挂牌日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['find:house:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['find:house:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['find:house:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['find:house:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="houseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="二手房源ID，主键" align="center" prop="houseId" />
      <el-table-column label="小区ID，外键关联小区表" align="center" prop="communityId" />
      <el-table-column label="成交价格" align="center" prop="averagePrice" />
      <el-table-column label="房略图" align="center" prop="imageUrl" />
      <el-table-column label="楼栋号" align="center" prop="buildingNo" />
      <el-table-column label="单元号" align="center" prop="unitNo" />
      <el-table-column label="楼层" align="center" prop="floorNo" />
      <el-table-column label="房号" align="center" prop="roomNo" />
      <el-table-column label="面积" align="center" prop="area" />
      <el-table-column label="朝向" align="center" prop="orientation" />
      <el-table-column label="室数量" align="center" prop="roomCount" />
      <el-table-column label="厅数量" align="center" prop="hallCount" />
      <el-table-column label="卫数量" align="center" prop="bathroomCount" />
      <el-table-column label="装修情况" align="center" prop="decoration" />
      <el-table-column label="挂牌价格" align="center" prop="price" />
      <el-table-column label="挂牌日期" align="center" prop="releaseDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.releaseDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="联系人" align="center" prop="contactPerson" />
      <el-table-column label="联系电话" align="center" prop="contactPhone" />
      <el-table-column label="最终价格" align="center" prop="hid" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['find:house:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['find:house:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改vr房源对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="小区ID，外键关联小区表" prop="communityId">
          <el-input v-model="form.communityId" placeholder="请输入小区ID，外键关联小区表" />
        </el-form-item>
        <el-form-item label="楼栋号" prop="buildingNo">
          <el-input v-model="form.buildingNo" placeholder="请输入楼栋号" />
        </el-form-item>
        <el-form-item label="单元号" prop="unitNo">
          <el-input v-model="form.unitNo" placeholder="请输入单元号" />
        </el-form-item>
        <el-form-item label="楼层" prop="floorNo">
          <el-input v-model="form.floorNo" placeholder="请输入楼层" />
        </el-form-item>
        <el-form-item label="房号" prop="roomNo">
          <el-input v-model="form.roomNo" placeholder="请输入房号" />
        </el-form-item>
        <el-form-item label="面积" prop="area">
          <el-input v-model="form.area" placeholder="请输入面积" />
        </el-form-item>
        <el-form-item label="朝向" prop="orientation">
          <el-input v-model="form.orientation" placeholder="请输入朝向" />
        </el-form-item>
        <el-form-item label="室数量" prop="roomCount">
          <el-input v-model="form.roomCount" placeholder="请输入室数量" />
        </el-form-item>
        <el-form-item label="厅数量" prop="hallCount">
          <el-input v-model="form.hallCount" placeholder="请输入厅数量" />
        </el-form-item>
        <el-form-item label="卫数量" prop="bathroomCount">
          <el-input v-model="form.bathroomCount" placeholder="请输入卫数量" />
        </el-form-item>
        <el-form-item label="装修情况" prop="decoration">
          <el-input v-model="form.decoration" placeholder="请输入装修情况" />
        </el-form-item>
        <el-form-item label="挂牌价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入挂牌价格" />
        </el-form-item>
        <el-form-item label="挂牌日期" prop="releaseDate">
          <el-date-picker clearable
            v-model="form.releaseDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择挂牌日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="联系人" prop="contactPerson">
          <el-input v-model="form.contactPerson" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="contactPhone">
          <el-input v-model="form.contactPhone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="${comment}" prop="hid">
          <el-input v-model="form.hid" placeholder="请输入${comment}" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listHouse, getHouse, delHouse, addHouse, updateHouse } from "@/api/find/house"

export default {
  name: "House",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // vr房源表格数据
      houseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        decoration: null,
        price: null,
        releaseDate: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        communityId: [
          { required: true, message: "小区ID，外键关联小区表不能为空", trigger: "blur" }
        ],
        area: [
          { required: true, message: "面积不能为空", trigger: "blur" }
        ],
        price: [
          { required: true, message: "挂牌价格不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询vr房源列表 */
    getList() {
      this.loading = true
      listHouse(this.queryParams).then(response => {
        this.houseList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        houseId: null,
        communityId: null,
        buildingNo: null,
        unitNo: null,
        floorNo: null,
        roomNo: null,
        area: null,
        orientation: null,
        roomCount: null,
        hallCount: null,
        bathroomCount: null,
        decoration: null,
        price: null,
        releaseDate: null,
        status: null,
        contactPerson: null,
        contactPhone: null,
        createdAt: null,
        updatedAt: null,
        hid: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.houseId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加vr房源"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const houseId = row.houseId || this.ids
      getHouse(houseId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改vr房源"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.houseId != null) {
            updateHouse(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addHouse(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const houseIds = row.houseId || this.ids
      this.$modal.confirm('是否确认删除vr房源编号为"' + houseIds + '"的数据项？').then(function() {
        return delHouse(houseIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('find/house/export', {
        ...this.queryParams
      }, `house_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
